package web.controller.user;

import common.consts.Consts;
import common.consts.ContentType;
import common.consts.Offers;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import service.OfferService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.security.Protection;

/**
 * Created by aceyin on 15/7/3.
 * 查看当前用户已经应聘的工作
 */
@RestController
@RequestMapping(Routes.User.OFFER)
public class Offer extends Base {
    private static final Logger LOGGER = LoggerFactory.getLogger(Offer.class);

    @Autowired
    private OfferService offerService;

    /**
     * 查看Offer
     *
     * @param id     - offer id
     * @param status
     * @param page
     * @return
     */
    @Protection
    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse view(@RequestParam(value = "id", defaultValue = "0") long id,
                                @RequestParam(value = "status", defaultValue = "") String status,
                                @RequestParam(value = "page", defaultValue = "1") int page) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Receiving offer request: id={}, status={}", id, status);
        }

        String stat = status;
        if (StringUtils.isBlank(status)) {
            stat = Offers.Status.EMPLOYED.name();
        }

        long user = getUserIdFromAuthHeader();

        if (id > 0) {
            return offerService.viewOffer(user, id);
        } else {
            return offerService.listOfferByStatus(user, stat, page, Consts.DEFAULT_ROWS);
        }
    }
}
